<template>

  <el-tabs type="border-card" :stretch="true">
    <el-tab-pane label="基础设置">
      <div>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
          <el-form-item label="产品名称" prop="id">
            <el-select v-model="queryParams.id" placeholder="请选择" @keyup.enter.native="handleQuery">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择" @keyup.enter.native="handleQuery">
              <el-option
                label="上架启用"
                value="0">
              </el-option>
              <el-option
                label="下架停用"
                value="1">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="productList">
          <el-table-column label="产品编码" align="center" prop="code"/>
          <el-table-column label="产品名称" align="center" prop="name"/>
          <el-table-column label="产品描述" align="center" prop="explain" :show-overflow-tooltip="true"/>
          <el-table-column label="产品类型" align="center" prop="type"/>
          <el-table-column label="上游价格" align="center" prop="originalPrice"/>
          <el-table-column label="本系统价格" align="center" prop="unitPrice"/>
          <el-table-column label="启用/停用" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="setStatus(scope.row)">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="移除/移入" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.del"
                active-value="0"
                inactive-value="1"
                @change="setDel(scope.row)">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:product:edit']">修改
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"/>
        <!-- 添加或修改查询产品对话框 -->
        <el-dialog title="修改查询产品" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="产品名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入产品名称"/>
            </el-form-item>
            <el-form-item label="产品描述" prop="explain">
              <el-input v-model="form.explain" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item label="上游价格" prop="originalPrice">
              <el-input v-model="form.originalPrice" placeholder="请输入上游价格"/>
            </el-form-item>
            <el-form-item label="本系统价格" prop="unitPrice">
              <el-input v-model="form.unitPrice" placeholder="请输入本系统价格"/>
            </el-form-item>
            <el-form-item label="默认展示" prop="defaultShow">
              <el-switch
                v-model="form.defaultShow"
                active-value="0"
                inactive-value="1">
              </el-switch>
            </el-form-item>
            <el-form-item label="默认可用">
              <el-switch
                v-model="form.defaultAvailable"
                active-value="0"
                inactive-value="1">
              </el-switch>
            </el-form-item>
            <el-form-item label="不可用提示">
              <el-input v-model="form.defaultNoAvailableMsg" placeholder="请输入不可用提示"/>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </el-tab-pane>
    <el-tab-pane label="开放设置">
      <enableList/>
    </el-tab-pane>
    <el-tab-pane label="定制价格">
      <price/>
    </el-tab-pane>
  </el-tabs>

</template>

<script>
  import enableList from "./enableList.vue";
  import price from "./price.vue";
  import {listProduct, getProduct, updateProduct} from "@/api/system/product";
  import {productName} from "@/api/system/product";

  export default {
    components: {enableList, price},
    name: "Product",
    data() {
      return {
        options: [],
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 查询产品表格数据
        productList: [],
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20,

          id: null,
          status: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            {required: true, message: "产品名称不能为空", trigger: "blur"}
          ],
          explain: [
            {required: true, message: "产品描述不能为空", trigger: "blur"}
          ],
          originalPrice: [
            {required: true, message: "上游价格不能为空", trigger: "blur"}
          ],
          unitPrice: [
            {required: true, message: "本系统价格不能为空", trigger: "blur"}
          ]
        }
      };
    },
    created() {
      this.getList();
      productName().then(response => {
        this.options = response.data;
      });
    },
    methods: {
      /** 查询查询产品列表 */
      getList() {
        this.loading = true;
        listProduct(this.queryParams).then(response => {
          this.productList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          explain: null,
          originalPrice: null,
          unitPrice: null,
          defaultShow: null,
          defaultAvailable: null,
          defaultNoAvailableMsg: '请联系客服开放'
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },

      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        getProduct(row.id).then(response => {
          this.form = response.data;
          this.open = true;
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateProduct(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      setStatus(row) {
        if (row.status == '1') {
          this.$prompt('请输入停用' + row.name + '提示', '停用提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            inputValue: row.remark
          }).then(({value}) => {
            updateProduct({
              id: row.id,
              status: row.status,
              remark: value
            })
            this.$modal.msgSuccess("已停用")
          }).catch(() => {
            this.$message({
              message: '取消输入'
            })
            row.status = row.status == '0' ? '1' : '0'
          })
        } else {
          this.$modal.confirm('确认要启用' + row.name + '吗？').then(function () {
            return updateProduct({
              id: row.id,
              status: row.status
            })
          }).then(() => {
            this.$modal.msgSuccess("启用成功")
          }).catch(function () {
            row.status = row.status == '0' ? '1' : '0'
          })
        }
      },
      setDel(row) {
        let text = row.del == '0' ? ' 移入 ' : ' 移除 '
        this.$modal.confirm('确认要' + text + row.name + '吗？').then(function () {
          return updateProduct({
            id: row.id,
            del: row.del
          })
        }).then(() => {
          this.$modal.msgSuccess(text + " 成功")
        }).catch(function () {
          row.del = row.del == '0' ? '1' : '0'
        })
      }
    }
  };
</script>
